<?php
/**
 * Created by PhpStorm.
 * User: Orange
 * Date: 2018/4/26
 * Time: 10:00
 */

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\utils\Constant;
use app\index\logic\MessageLogic;
use think\Db;

/**
 * 审核管理
 * Class verify
 * @package app\admin\controller
 */
class verify extends Backend
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('VerifyLog');
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->where($where)
                ->field(['password', 'salt', 'token'], true)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $key => &$value) {
                $nickName = model('Member')->where(array('id' => $value['member_id']))->value('name');
                $value['member_name'] = $nickName;
            }
            unset($value);
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 审核
     * @param string $ids
     * @return string
     */
    public function verify($ids = '')
    {
        if ($this->request->isAjax()) {
            Db::startTrans();
            try {
                $verify_log = $this->model->where(array('id' => $ids))->find();
                $bacteria = model('Bacteria');
                $map['id'] = $verify_log['bacteria_id'];
                $in_review = $bacteria->where($map)->value('in_review');
                if ($verify_log['status'] != Constant::VERIFY_STATUS_WAIT) {
                    $this->error('该记录已经审核过，无法重复审核!');
                }
                $data = $this->request->post("row/a");
                if (!isset($data['status']) || $data['status'] == '') {
                    $this->error('请选择是否通过审核!');
                }
                if ($data['status'] == Constant::VERIFY_STATUS_FAIL) {
                    //审核不通过，菌种审核中数量-1
                    if ($verify_log['type'] == 1) {
                        $bacteria->where($map)->update(array('in_review' => $in_review - 1));

                    }
                } else {
                    //审核通过，新建菌种数据
                    if ($verify_log['type'] == 0) {
                        $newBacteria = $bacteria->where('chinese_name = :chinese_name OR the_latin_name = :the_latin_name', ['chinese_name' => $verify_log['chinese_name'], 'the_latin_name' => $verify_log['the_latin_name']])
                            ->where(array('is_use' => 1))->find();
                        if ($newBacteria) {
                            $this->error('资料库中已存在改菌种无法新建');
                        } else {
                            $bacteria->where($map)->update(array('is_use' => 1));
                        }
                    } //审核通过 菌种审核中-1 已有+1
                    else {
                        $bacteria->where($map)->update(array('in_review' => $in_review - 1));
                        $amount = $bacteria->where($map)->value('amount');
                        $bacteria->where($map)->update(array('amount' => $amount + 1));
                    }
                }
                $this->model->where(['id' => $ids])->update($data);
                $user_id = model('Member')->where(array('id' => $verify_log['member_id']))->value('user_id');
                Db::commit();
                //发送通知
                if ($user_id != 0) {
                    $message_logic = new MessageLogic();
                    if ($data['status'] == Constant::VERIFY_STATUS_FAIL) {
                        $message = '您提交的'. $verify_log['chinese_name'].'的审核不通过';
                    } else {
                        $message = '您提交的'. $verify_log['chinese_name'].'的审核已被通过';
                    }
                    $message_logic->sendMessage($user_id, $message);
                }

                $this->success('审核成功！');
            } catch (Exception $e) {
                Db::rollback();
                $this->error('审核失败!', '', $e->getMessage());
            }
        }
        return $this->view->fetch();
    }

    /**
     * 查看详情
     * @param string $ids
     * @return string
     */
    public function detail($ids = '')
    {
        $bacteriaId = $this->model->where(array('id' => $ids))->value('bacteria_id');
        $categoryData = model('BacteriaCategory')->where(array('bacteria_id' => $bacteriaId))->select();
        for ($level = 0; $level < 7; $level++) {
            $id = $level + 1;
            $row['category_id' . $id] = $categoryData[$level]['category_id'];
        }
        $this->assignconfig('bacteria', $row);
        return $this->view->fetch();
    }

}